import Cookies from 'js-cookie'
import { switchFontSize } from '@/utils/switch-font-size'
const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'mini',
  settingsOpened: false,
  searchColNumber: 6,
  fontSizeBase: parseInt(Cookies.get('fontSizeBase')) || 14,
  residentAddressTree: Cookies.get('tree') || '1', // 常用地址树显示
  autoGuide: Cookies.get('autoGuide') || '0', // 自动启动指南
  menuFont: Cookies.get('menuFont') || '0' // 菜单字体大小是否与页面一致1为一致0为不一致
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  },
  SET_FONT_SIZE_BASE: (state, fontSizeBase) => {
    state.fontSizeBase = fontSizeBase
    switchFontSize(fontSizeBase)
    Cookies.set('fontSizeBase', fontSizeBase)
  },
  START_AUTO_GUIDE: (state) => {
    state.autoGuide = '1'
    Cookies.set('autoGuide', '1')
  },
  SET_SETTINGS: (state, opened) => {
    state.settingsOpened = opened
  },
  SET_SEARCH_COL: (state, span) => {
    state.searchColNumber = span
  },
  SET_RESIDENT_ADDRESS_TREE: (state, residentAddressTree) => {
    state.residentAddressTree = residentAddressTree
    Cookies.set('tree', residentAddressTree)
  },
  SET_MENUFONT: (state, menuFont) => {
    state.menuFont = menuFont
    Cookies.set('menuFont', menuFont)
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
  setFontSizeBase({ commit }, fontSizeBase) {
    commit('SET_FONT_SIZE_BASE', fontSizeBase)
  },
  setResidentAddressTree({ commit }, residentAddressTree) {
    commit('SET_RESIDENT_ADDRESS_TREE', residentAddressTree)
  },
  closeSettings({ commit }) {
    commit('SET_SETTINGS', false)
  },
  openSettings({ commit }) {
    commit('SET_SETTINGS', true)
  },
  setSpan({ commit }, span) {
    commit('SET_SEARCH_COL', span)
  },
  startGuide({ state, commit }) {
    if (state.autoGuide === '0') {
      commit('START_AUTO_GUIDE')
      return true
    } else {
      return false
    }
  },
  setMenuFont({ commit }, menuFont) {
    commit('SET_MENUFONT', menuFont)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
